import StringUtils from "@/utils/StringUtils";
import menuHandler from "@/common/menu/menuHandler";

const ignores = require("dev/data/ignores.json");
const menus = menuHandler.loadMenus();

const develop = {
	loadPages() {
		let mainPages = [];
		menus.forEach(menu => {
			if (menu.id.toString() !== "0") {
				let mainPage = {
					id: menu.id,
					name: menu.name,
					title: menu.title,
					icon: menu.icon,
					level: menu.level,
					hasChildren: true,
					children: this.searchChildPages(menu, null, [])
				};
				mainPages.push(mainPage);
			}
		});
		return mainPages;
	},

	loadRoutes() {
		let routeData = [];
		menus.forEach(menu => {
			let mainPage = {
				id: menu.id,
				name: menu.name,
				title: menu.title,
				icon: menu.icon,
				level: menu.level,
				hasChildren: true,
				children: this.searchChildRoutes(menu, null, [])
			};
			routeData.push(mainPage);
		});
		console.log(routeData);
		return routeData;
	},

	searchChildPages(menu, item, children) {
		let items = item !== null ? item : menu;
		if (menuHandler.hasChildren(items)) {
			items.children.forEach(child => {
				if (child.id.toString() !== "1-1" && ignores.indexOf(child.id.toString()) === -1) {
					if (child.path !== null && child.path !== "") {
						this.loadChildPages(child, menu, item, children);
					} else {
						let bottomChildren = this.searchChildPages(menu, child, []);
						if (bottomChildren && bottomChildren.length > 0) {
							let slavePage = {
								id: child.id,
								name: child.name,
								title: child.title,
								icon: child.icon,
								level: child.level,
								hasChildren: Number(child.level) !== 3,
								children: bottomChildren
							};
							children.push(slavePage);
						}
					}
				}
			});
		}
		return children;
	},

	searchChildRoutes(menu, item, children) {
		let items = item !== null ? item : menu;
		if (menuHandler.hasChildren(items)) {
			items.children.forEach(child => {
				if (child.path !== null && child.path !== "") {
					this.loadChildRoutes(child, menu, item, children);
				} else {
					let bottomChildren = this.searchChildRoutes(menu, child, []);
					if (bottomChildren && bottomChildren.length > 0) {
						let slaveRoute = {
							id: child.id,
							name: child.name,
							title: child.title,
							icon: child.icon,
							level: child.level,
							hasChildren: Number(child.level) !== 3,
							children: bottomChildren
						};
						children.push(slaveRoute);
					}
				}
			});
		}
		return children;
	},

	loadChildRoutes(child, menu, item, children) {
		let childRoute = {
			id: Number(child.level) === 2 ? child.id + "-1" : child.id,
			title: child.title,
			name: child.name,
			icon: child.icon,
			root: menu.name,
			parent: item !== null ? item.name : (child.id.toString() === "1-1" ? "menus" : ""),
			routeName: "",
			routePath: ""
		};
		children.push(childRoute);
	},

	loadChildPages(child, menu, item, children) {
		let operate = child.name.indexOf("query") !== -1 ? "table" : (child.name.indexOf("form") !== -1 ? "form" : "blank");
		let childName = child.name.indexOf("-") !== -1 ? child.name.split("-")[0] : child.name;
		let fileName = StringUtils.upperCamelCase(childName);
		let folder = item == null ? menu.name + "/" : menu.name + "/" + item.name + "/";
		let childPage = {
			id: child.id,
			title: child.title,
			name: child.name,
			icon: child.icon,
			operate: operate,
			folder: folder,
			componentName: child.name,
			fileName: fileName,
			isUpdate: false,
			params: []
		};
		children.push(childPage);
	}
};
export default develop;
